package org.shj.tdpl.loader.config;

import org.shj.tdpl.loader.loader.DataLoader;
import org.shj.tdpl.loader.loader.excel.AbstractXlsxDataLoader;
import org.shj.tdpl.loader.loader.excel.MultiFileDataLoader;
import org.shj.tdpl.loader.loader.excel.SingleFileDataLoader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.scheduling.annotation.EnableAsync;

/**
 *
 * @author ShenHuangJian
 * @date 2018年6月28日
 *
 */
@Configuration
@ComponentScan
@EntityScan
public class TdlConfig {
	
	@Value("${spring.datasource.url}")
	private String url;
	
	@Value("${spring.datasource.username}")
	private String username;
	
	@Value("${spring.datasource.password}")
	private String password;
	
	@Value("${tdpl.schema.type:multiple}")
	private String fileType;
	
	@Value("${tdpl.schema.folder}")
	private String testDataFolder;
	
	@Bean
	public DataLoader loader() {
		DriverManagerDataSource dataSource = new DriverManagerDataSource(url, username, password);
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		
		if("multiple".equalsIgnoreCase(fileType)) {
			AbstractXlsxDataLoader loader = new MultiFileDataLoader(); 
			loader.setJdbcTemplate(jdbcTemplate);
			loader.setGeneratedFilePath(testDataFolder);
			return loader;

		}else if("single".equalsIgnoreCase(fileType)){
			AbstractXlsxDataLoader loader = new SingleFileDataLoader();
			loader.setJdbcTemplate(jdbcTemplate);
			loader.setGeneratedFilePath(testDataFolder);
			return loader;
		}
		throw new RuntimeException("schema 现在只支持 multiple 和 single 形式。");
	}
}
